package kgame5.pbe.atlantic.comp {
	import kgame5.pbe.atlantic.event.ViewEvent;
	import kgame5.pbe.atlantic.fsm.FSM;
	import kgame5.pbe.atlantic.fsm.GState;
	import kgame5.pbe.atlantic.param.FSMParam;
	import kgame5.pbe.atlantic.vo.EntityVO;
	import kgame5.pbe.engine.components.TickedComponent;

	import flash.utils.Dictionary;
	import flash.utils.getDefinitionByName;

	/**
	 * @author max
	 * 2010-12-13
	 * 并行状态机，根据fsmParam的列表来构造
	 */
	public class FSMBatchComp extends TickedComponent {
		//保持各个fsm用来遍历用,并行状态机
		private var dict_fsm : Dictionary;//dict_fsm[fsmID]=FSM
		
		//param是FSMParam的数组
		public function FSMBatchComp(vo:EntityVO,params:Array) {
			dict_fsm = new Dictionary();
			
			//根据参数构造fsm
			for each(var param:FSMParam in params){
				var fsm:FSM=new FSM(param.fsmID,vo);
				for(var stateID:int in param.dict_state){
					var StateClass:Class=getDefinitionByName(param.dict_state[stateID]) as Class;
					var s:GState=new StateClass(stateID,fsm,vo);
					fsm.addState(s);
				}
				dict_fsm[fsm.getID()]=fsm;
			}
		}
		//为每个fsm设置eventDipatcher,以便发送事件
		override protected function onAdd() : void {
			super.onAdd();
			
			for each(var fsm:FSM in dict_fsm){
				fsm.setEventDispatcher(owner.eventDispatcher);
			}
		}
		
		//更新状态机
		override public function onTick(deltaTime : Number) : void {
			for each(var fsm:FSM in dict_fsm) {
				fsm.update();
			}
		}
		
		//切换某个fsm到特定状态
		public function SwitchFSMState(fsmID : int,stateID : int,data : Object = null) : void {
			var fsm : FSM = findFSM(fsmID);
			if(fsm != null) {
				fsm.SwitchState(stateID, data);
			}
		}
		
		//-
		private function findFSM(fsmID : int) : FSM {
			return dict_fsm[fsmID];
		}
		
		//-很实用的两个函数，由ViewComp通知Controller而传入这里，最终由当前GState处理
		public function click(e:ViewEvent):void{
			for each(var fsm:FSM in dict_fsm){
				var x:int=e.data.x;
				var y:int=e.data.y;
				fsm.getCurState().click(x,y);
			}
		}
		public function endOFrame(e:ViewEvent):void{
			for each(var fsm:FSM in dict_fsm){
				var animName:String = e.data as String;
				fsm.getCurState().endOFrame(animName);
			}
		}
	}//end of class
}

//		//==================操作FSMS=================
//		//子类如果需要fsm通知guActor,可以覆写此函数
//		//注意，添加时fsm需要向Entity注册监听事件
//		private function addFSM(fsm : FSM) : FSM {
//			if(dict_fsm[fsm.getID()] != null) {
//				throw new Error("已含有此fsm");
//			}
//			dict_fsm[fsm.getID()] = fsm;
//			return fsm;
//
//		}		
//		override protected function onAdd() : void {
//			super.onAdd();
//		}
//
//		override protected function onRemove() : void {
//			super.onRemove();
//		}
//
//		override protected function onReset() : void {
//			super.onReset();
//		}

		


		//从没用到过
		//	public function removeFSM(fsmID:int):void{
		//		delete dict_fsm[fsmID];
		//	}
		//这个在各个Entity里设置
//		fsm.addEventListener(FSMEvent.TYPE, onFSMEvent);
//		fsm.addEventListener(SendCommandEvent.TYPE, onSendCommandEvent);
//		
//		//暂存一下，方便以后传递到State中去
//		fsm.setVO(this.vo);
//		fsm.setHardVO(this.hard);
		//	//设置到各个state里的ISceneWorld
		//	public function setSenceWorld(senceWorld:ISenceWorld):void{
		//		for each(var fsm:FSM in dict_fsm){
		//			fsm.setSenceWorld(senceWorld);
		//		}
		//	}
		//	//当Entity被添加到世界后，被world调用，传入
		//	public function setSpacialManager(spacialManager:SpacialManager):void{
		//		for each(var fsm:FSM in dict_fsm){
		//			fsm.setSpacialManager(spacialManager);
		//		}
		//	}